<?php

class NotifyRelationshipLwModel extends LW_Model {
	public $type = "ts_comment";
     public $table_name = "notify_relationship";
	public function addRelation($commentId,$notifyId,$uid=null,$type=null){
		$map['notifyId'] = $notifyId;
		$map['relationshipId'] = $commentId;
		isset($uid) && $map['uid'] = $uid;
		isset($type) && $map['type'] = $type;
		$result =$this->add($map);
		return $result;
	}
	
	public function findRelation($notifyId,$uid=null){
		$map['notifyId'] = $notifyId;
		$map['type'] = $this->type;
		isset($uid) && $map['uid'] = array('neq',$uid);
		return $this->tidyData($map,'relationshipId');
	}
	
	public function findNotify($commentId,$uid=null){
		if(is_array($commentId)){
			$map['relationshipId'] = array('in',$commentId);
		}else{
			$map['relationshipId'] = $commentId;
		}
		$map['type'] = $this->type;
		isset($uid) && $map['uid'] = array('neq',$uid);
		return $this->tidyData($map,'notifyId');
	}
	
	public function del($id,$field="notifyId",$type="ts_comment"){
		if(is_array($id)){
			$map[$field] = array('in',$id);
			$notifyId = $this->where($map)->field('notifyId')->findAll();
			foreach($notifyId as $value){
				$deleteNotifyId = $value['notifyId'];
			}
			$dao = TS_D('Notify');
			$map2['id'] = array('in',$deleteNotifyId);
			$dao->where($map2)->delete();
		}else{
			$map[$field]= $id;
			$notifyId = $this->where($map)->field('notifyId')->find();
			$dao = TS_D('Notify');
			$map2['id'] = $notifyId['notifyId'];
			$dao->where($map2)->delete();
		}
		$map['type'] = $type;
		

		$result = $this->where($map)->delete();
		dump($this->getLastSql());
		return $result;
	}
	
	private function tidyData($map,$string){
		$result = $this->where($map)->field("{$string},uid,relationshipId as id")->findAll();
		return $result;
	}
}

?>